package com.zch.news.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zch.news.entity.News;
import com.zch.news.entity.User;
import com.zch.news.service.NewsService;
import com.zch.news.service.UserService;
import com.zch.news.service.impl.NewsServiceImpl;
import com.zch.news.service.impl.UserServiceImpl;

/**
 * Servlet implementation class NewsServlet
 */
@WebServlet("/NewsServlet")
public class NewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//实例化一个UserService的实现类对象
	UserService us = new UserServiceImpl();
    NewsService ns = new NewsServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewsServlet() {
        super();
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.service(request, response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//解决中文乱码
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		if("add".equals(method)) {
			add(request, response);
		}else if("login".equals(method)) {
			login(request, response);
		}else if("updateUser".equals(method)) {
			updateUser(request, response);
		}else if("updateNews".equals(method)) {
			updateNews(request, response);
		}else if("deleteNews".equals(method)) {
			deleteNews(request, response);
		}else if("addNews".equals(method)) {
			addNews(request, response);
		}
	}
	//添加用户
	protected void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String phone = request.getParameter("userPhone");
		String password = request.getParameter("userPassword");
		//实例化一个User对象
		User user = new User();
		//封装对象属性
		user.setPhone(phone);
		user.setPassword(password);
		if("18839137398".equals(phone)||"18224671354".equals(phone)||"18224655279".equals(phone)) {
			user.setRole(0);//管理员
		}else {
			user.setRole(1);//普通用户
		}
		//向数据库中添加用户信息
		boolean flag = us.addUser(user);
		if(flag) {
			response.sendRedirect("register_success.jsp");
		}		
	}
	
	//修改用户密码
	protected void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String phone = request.getParameter("userPhone");
		String password = request.getParameter("userPassword");
		//修改用户密码
		User user = us.getUserByPhone(phone);
		user.setPassword(password);
		us.updateUser(user);
		response.sendRedirect("login.jsp");
	}
	
	//用户类型登录判断
	protected void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String phone = request.getParameter("userPhone");
		String password = request.getParameter("userPassword");
		//获得登录信息判断的一个map集合
		Map<String, Object> map = us.login(phone, password);
		//验证是否登录成功
 		boolean isLogin = (Boolean)map.get("flag");
 		if(isLogin){
 			//获得一个session对象
 			HttpSession session = request.getSession();
 			//获得所有新闻消息
 		    List<News> list = ns.getAllNews();
 		    session.setAttribute("list", list);
 			//用户存在且密码正确
 			//获得登录的用户对象
 			User user = (User) map.get("user1");
 			//使用session存储得到的用户对象
 			session.setAttribute("user1", user);
 			if(user.getRole() == 0) {//管理员
 				response.sendRedirect("manager.jsp");
 			}else {//一般用户
 				response.sendRedirect("user.jsp");
 			}	
 		}else{
 			//登录失败，返回到login.jsp页面
 			//获取错误信息
 			Object msg = map.get("errMsg");
 			//把错误信息放到请求的作用域里面
 			request.setAttribute("error", msg);
 			//利用转发把错误信息转发到下一个页面
 			request.getRequestDispatcher("login.jsp").forward(request, response);
 		}
	}
	//更新新闻
	protected void updateNews(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String author = request.getParameter("author");
		String createTime = request.getParameter("createTime");
		String no = request.getParameter("newsNo");
		int no2 = Integer.parseInt(no);
		News news = ns.getNewsByNo(no2);
		news.setContent(content);
		news.setTitle(title);
		news.setAuthor(author);
		news.setCreatTime(createTime);
		//更新新闻
		ns.updateNews(news);
			//获得一个session对象
			HttpSession session = request.getSession();
			//获得所有新闻消息
		    List<News> list = ns.getAllNews();
		    session.setAttribute("list", list);
		response.sendRedirect("manager.jsp");
	}
	
	//删除新闻
	protected void deleteNews(HttpServletRequest request, HttpServletResponse response) throws IOException {
	String no = request.getParameter("newsNo");
	int no2 = Integer.parseInt(no);
	//删除新闻
	ns.deleteNews(no2);
	//获得一个session对象
	HttpSession session = request.getSession();
	//获得所有新闻消息
    List<News> list = ns.getAllNews();
    session.setAttribute("list", list);
	response.sendRedirect("manager.jsp");
	}
	
	//添加新闻
	protected void addNews(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int no = Integer.parseInt(request.getParameter("newsNo"));
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String author = request.getParameter("author");
		String creatTime = request.getParameter("createTime");
		News news = new News(title, content, author, creatTime);
		news.setNo(no);
		//添加新闻
		ns.addNews(news);
		//获得一个session对象
		HttpSession session = request.getSession();
		//获得所有新闻消息
	    List<News> list = ns.getAllNews();
	    session.setAttribute("list", list);
		response.sendRedirect("manager.jsp");
	}
}
